﻿using System;
using Team4Mock.Entity;
using System.Data;

namespace Team4Mock.UserControl.TrustDistrict
{
    public partial class AddDistrict : System.Web.UI.UserControl
    {
        TrustDistrictEntity te=new TrustDistrictEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int idregion = int.Parse(Request.QueryString["t"]);
                te.TrustRegionId = idregion;
                txtRegionName.Text = te.GetNameRegion(te);

                //Amend
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    int iddis = int.Parse(Request.QueryString["ID"]);
                    te.TrustDistrictId = iddis;
                    DataTable dt = new DataTable();
                    dt = te.GetdataByDIs(te);
                    txtDisName.Text = dt.Rows[0][1].ToString();
                    txtDes.Text = dt.Rows[0][2].ToString();
                    if (dt.Rows[0][4].ToString() == "Yes")
                    {
                        ckisactive.Checked = true;
                    }
                    else
                    {
                        ckisactive.Checked = false;
                    }
                }
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/TrustRegion.aspx?ID=" + Request.QueryString["t"]);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Amend
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int id=int.Parse(Request.QueryString["ID"]);
                if (txtDisName.Text=="")
                {
                    tb.Text = "Input District Name";
                }
                else
                {
                    te.TrustDistrictId = id;
                    te.TrustDistrictName = txtDisName.Text;
                    te.Description = txtDes.Text;
                    te.IsActive = ckisactive.Checked ? 1 : 0;
                    te.Update(te);
                    tb.Text = "Update Success";
                }
                
            }
            //Add
            else
            {
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    int id = int.Parse(Request.QueryString["t"]);
                    if (txtDisName.Text == "")
                    {
                        tb.Text = "Input District Name";
                    }
                    else
                    {
                        te.TrustDistrictName = txtDisName.Text;
                        te.Description = txtDes.Text;
                        te.TrustRegionId = id;
                        te.IsActive = ckisactive.Checked ? 1 : 0;
                        te.Insert(te);
                        tb.Text = "Insert Success";
                    }
                }
            }
        }
    }
}